<?php

// +----------------------------------------------------------------------
// | 用户权限管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\ucenter\admin;

use app\controller\admin\Common;
use app\model\ucenter\UserAuth as UserAuthModel;
use app\event\ucenter\UserAuth as UserAuthEvent;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;

class UserAuth extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $map = [];
        if ($this->request->has('status') && !$this->request->has('field.status')) {
            $map[] = ['status', '=', $this->request->param('status')];
        } elseif (!$this->request->has('field.status')) {
            $map[] = ['status', '=', 0];
        } else {
//                $map[] = ['status', '=', 0];
        }
        return $map;
    }

    public function index()
    {
        // 显示页面
        $statusOptions = [
            [
                'id' => -1,
                'value' => '已拒绝',
            ],
            [
                'id' => 0,
                'value' => '待审核',
            ],
            [
                'id' => 1,
                'value' => '已经通过',
            ]
        ];
        // 显示页面
        $builder = new Table();
        $builder->model(UserAuthModel::class)
            ->where($this->_filter())
            ->searchSelect('status', '审核状态：', $statusOptions)
            ->order('id DESC')
//				->suggest('自动审核未通过用户可以提交待审核')
            ->searchText('user_id', '用户ID', '需要精确查询')
            ->keyId()
            ->keyUser('user_id', '提交用户')
            ->keyText('user.email', '邮箱')
            ->keyText('idcard_name', '认证姓名', false, 100)
//                ->keyText('idcard_no', '身份证号')
//                ->keyImageModel('front|front_id', '正面身份证')
//                ->keyImageModel('back|back_id', '反面身份证')
//                ->keyImageModel('hand|hand_id', '手持身份证')
//                ->keyText('message', '用户留言')
            ->keyTime('create_time', '提交时间')
            ->keyText('status_info', '状态')
            ->actionUpdate('ucenter/admin/UserAuth/update?id={$id}', '审核', [0]);
        return $builder->fetch();
    }

    public function update()
    {
        if ($this->request->isPost()) {
            $event = new UserAuthEvent();
            $result = $event->examine($this->request->post(), $this->user);
            if (0 == $result['code']) {
                $this->_returnSuccess($result['message'], true);
            } else {
                $this->_returnError($result['message']);
            }
        } else {
            $auth = UserAuthModel::where('id', $this->request->param('id'))->find();
            if (!$auth) {
                $this->_returnError('提交信息不存在');
            }
//                $user = UserModel::where('id', $auth['user_id'])->find();
//				$user= $user->toArray();
//                $user['idcard_name'] = $auth['idcard_name'];
//				dump($auth->toArray());
            $auth['nickname'] = $auth->user['nickname'];
            $auth['email'] = $auth->user['email'];
            $builder = new Edit();
            $builder->title('审核认证用户');
            $builder->keyHidden('auth.id')
                ->keyLabel('nickname', '用户昵称', '用户昵称')
                ->keyLabel('email', '邮箱', '')
                ->keyTextInline('idcard_name', '认证名')
//                    ->keyTextInline('auth.idcard_name', '身份证姓名', '实名认证后需要绑定同名支付宝进行提现', '', 'required|chs')
//                    ->keyTextInline('auth.idcard_no', '身份证号', '', '', 'required,identity')
//                    ->keyAuth()
//                    ->keyText('auth.auth_info', '认证信息', '')
//                    ->keyLabel('auth.message', '用户留言')
                ->keyBool('examine', '是否通过认证', '', 1)
//                    ->keyTextInline('refuse', '审核结果', '不通过请写明原因')
//                    ->keyText('remark', '备注', '自动审核未通过结果')
                ->keyTextArea('result', '拒绝原因', '如果拒绝填写')
                ->data($auth)
                ->buttonSubmit('', '提交');
            return $builder->fetch();
        }
    }

}
